<?php

abstract class CDBModel {
	
	private $_transactionDb = array();
	
	private static $_db=array();
	
	protected abstract function getTabelName();
	
	protected abstract function getPKID();
	
	function __get($name)
	{
		if($name==="db")
		{
			return $this->db();
		}
		else if($name==="proc")
		{
			return $this->proc();
		}
	}
	
	protected function db($dbConfigKey="server")
	{
		return $this->getInstance($dbConfigKey, false);
	}
	
	protected function proc($dbConfigKey="server")
	{
		return $this->getInstance($dbConfigKey, true);
	}
	
	private function getInstance($dbConfigKey,$isProc)
	{
		$db = null;
		if(isset(self::$_db[$dbConfigKey]) && self::$_db[$dbConfigKey])
		{
			$db = self::$_db[$dbConfigKey];
		}
		else
		{
			$db = $this->dbDriver($dbConfigKey);
			
			self::$_db[$dbConfigKey] = $db;
		}
		
		$db->setProcFlag($isProc);
		
		if(in_array($dbConfigKey,$this->_transactionDb))
		{
			if(!$db->hasBeginTransaction())
			{
				$db->beginTransaction();
			}
			
			//判断在已有的数据库链接里是否已经回滚事务,如已回滚,则当前请求的链接事务回滚
			foreach (self::$_db as $dbItem)
			{
				if($dbItem->hasBeginTransaction() && $dbItem->hasRollBack())
				{
					$db->rollBack();
					break;
				}
			}
		}
		
		return $db;
	}
	
	/**
	 * 开始一个事务,该事务可以执行sql语句也可以执行存储过程
	 */
	public function beginTransaction($dbConfigKey="server")
	{
		if($dbConfigKey)
		{
			$this->_transactionDb = explode(",",$dbConfigKey);
		}
		else
		{
			$this->_transactionDb = array();
		}
	}
	
	/**
	 * 提交事务
	 */
	public function commit()
	{
		$hasRollback = false;
		foreach($this->_transactionDb as $db)
		{
			if(isset(self::$_db[$db]))
			{
				if(self::$_db[$db]->hasRollBack())
				{
					$hasRollback = true;
					break;
				}
			}
		}
		//var_dump($hasRollback);
		if($hasRollback)
		{
			$this->rollBack();
		}
		else
		{
			foreach($this->_transactionDb as $db)
			{
				if(isset(self::$_db[$db]))
				{
					self::$_db[$db]->commit();
				}
			}
			
			//$this->_transactionDb = array();
		}
		/*if(self::$_db)
		{
			self::$_db->commit();
		}*/
	}
	
	/**
	 * 事务回滚
	 */
	public function rollBack()
	{
		foreach($this->_transactionDb as $db)
		{
			if(isset(self::$_db[$db]))
			{
				self::$_db[$db]->rollBack();
			}
		}
	}
	
	/**
	 * 返回一个bool类型的值标识事务提交是否成功
	 */
	public function commitSuccess()
	{
		foreach($this->_transactionDb as $db)
		{
			if(isset(self::$_db[$db]))
			{
				if(self::$_db[$db]->commitSuccess() == false)
				{
					return false;
				}
			}
		}
		
		return true;

	}
	
	private function dbDriver($dbConfigKey)
	{
		$driver = Config::get("db.driver","");
		
		if(!$driver)
		{
			CError::handler(E_ERROR,"数据库驱动配置错误",__FILE__,__LINE__);
		}
		
		if($driver!=="mysql" && $driver!=="oracle")
		{
			CError::handler(E_ERROR,"数据库驱动[".$driver."]不存在",__FILE__,__LINE__);
		}

		$driver = "C".ucwords($driver);
		
		return new $driver($dbConfigKey);
	}
}

?>